package org.rea.GUI;

import java.awt.GraphicsConfiguration;
import java.awt.GridLayout;
import java.awt.HeadlessException;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;

import org.rea.logic.Database;

public class InsertNew extends JFrame {
	private HashMap<String, JTextField> _tfldColumn;
	private JButton _btnSubmit;

	public InsertNew(final String table) throws SQLException {

		setLayout(new GridLayout(0, 2));

		PreparedStatement pst = Database.getInstance().getConnection()
				.prepareStatement("select * from " + table);
		final ResultSetMetaData data = pst.executeQuery().getMetaData();

		_tfldColumn = new HashMap<String, JTextField>();
		for (int i = 1; i <= data.getColumnCount(); i++) {
			System.out.println(data.getColumnName(i));
			add(new JLabel(data.getColumnName(i)));

			_tfldColumn.put(data.getColumnName(i), new JTextField());

			add(_tfldColumn.get(data.getColumnName(i)));
		}

		_btnSubmit = new JButton("Submit");
		_btnSubmit.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				StringBuffer sql = new StringBuffer("INSERT INTO " + table
						+ " VALUES (");

				try {
					for (int i = 1; i < data.getColumnCount(); i++) {
						sql.append("?, ");
					}

					sql.append("?)");
					System.out.println("SQL : " + sql.toString());
					PreparedStatement pst = Database.getInstance().getConnection().prepareStatement(sql.toString());
				} catch (SQLException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				}
			}
		});

		add(_btnSubmit);
		pack();
	}

}